//
//  TFTableViewCollectionItemLayout.m
//  AppLibs
//
//  Created by 杜恺 on 2018/7/30.
//  Copyright © 2018年 jack Kong. All rights reserved.
//

#import "TFTableViewCollectionItemLayout.h"

@interface TFTableViewCollectionItemLayout ()

@property (nonatomic, strong, readwrite) NSArray *itemFramesArray;

@end

@implementation TFTableViewCollectionItemLayout

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.totalWidth = _TFScreenSize().width;
    }
    return self;
}

- (instancetype)initWithItemIntrinsicSize:(CGSize)intrinsicSize
                       minHorizontalSpace:(CGFloat)minHorizontalSpace
                            verticalSpace:(CGFloat)verticalSpace {
    self = [super init];
    if (self) {
        self.totalWidth = _TFScreenSize().width;
        self.itemIntrinsicWidth = intrinsicSize.width;
        self.itemIntrinsicHeight = intrinsicSize.height;
        self.itemHorizontalSpace = minHorizontalSpace;
        self.itemVerticalSpace = verticalSpace;
    }
    return self;
}

- (instancetype)initWithItemIntrinsicSize:(CGSize)intrinsicSize listCount:(NSInteger)listCount verticalSpace:(CGFloat)verticalSpace {
    self = [super init];
    if (self) {
        self.totalWidth = _TFScreenSize().width;
        self.itemIntrinsicWidth = intrinsicSize.width;
        self.itemIntrinsicHeight = intrinsicSize.height;
        self.listCount = listCount;
        self.itemVerticalSpace = verticalSpace;
    }
    return self;
}

- (instancetype)initWithItemIntrinsicWidth:(CGFloat)intrinsicWidth listCount:(NSInteger)listCount verticalSpace:(CGFloat)verticalSpace intrinsicHeightRatioToWidth:(CGFloat)intrinsicHeightRatioToWidth {
    self = [super init];
    if (self) {
        self.totalWidth = _TFScreenSize().width;
        self.itemIntrinsicWidth = intrinsicWidth;
        self.itemIntrinsicHeightRatioToWidth = intrinsicHeightRatioToWidth;
        self.listCount = listCount;
        self.itemVerticalSpace = verticalSpace;
    }
    return self;
}

- (instancetype)initWithItemListCount:(NSInteger)listCount horizontalSpace:(CGFloat)horizontalSpace verticalSpace:(CGFloat)verticalSpace intrinsicHeight:(CGFloat)intrinsicHeight {
    self = [super init];
    if (self) {
        self.totalWidth = _TFScreenSize().width;
        self.listCount = listCount;
        self.itemHorizontalSpace = horizontalSpace;
        self.itemVerticalSpace = verticalSpace;
        self.itemIntrinsicHeight = intrinsicHeight;
    }
    return self;
}

- (instancetype)initWithItemListCount:(NSInteger)listCount horizontalSpace:(CGFloat)horizontalSpace verticalSpace:(CGFloat)verticalSpace intrinsicHeightRatioToWidth:(CGFloat)intrinsicHeightRatioToWidth {
    self = [super init];
    if (self) {
        self.totalWidth = _TFScreenSize().width;
        self.listCount = listCount;
        self.itemHorizontalSpace = horizontalSpace;
        self.itemVerticalSpace = verticalSpace;
        self.itemIntrinsicHeightRatioToWidth = intrinsicHeightRatioToWidth;
    }
    return self;
}



- (instancetype)initWithItemCustomItemWidthBlock:(TFTableViewCollectionItemLayoutCustomItemWidthAtIndexBlock)customItemWidthAtIndexBlock horizontalSpace:(CGFloat)horizontalSpace verticalSpace:(CGFloat)verticalSpace intrinsicHeight:(CGFloat)intrinsicHeight {
    self = [super init];
    if (self) {
        self.totalWidth = _TFScreenSize().width;
        self.customItemWidthAtIndexBlock = customItemWidthAtIndexBlock;
        self.itemHorizontalSpace = horizontalSpace;
        self.itemVerticalSpace = verticalSpace;
        self.itemIntrinsicHeight = intrinsicHeight;
    }
    return self;
}

- (void)layout {
    NSMutableArray *itemFramesArray = [NSMutableArray array];
    if (self.itemCount) {
        CGFloat totalWidth = self.totalWidth - self.marginInsets.left - self.marginInsets.right;
        if (self.itemIntrinsicWidth) {
            if (self.listCount>1) {
                //平分间距
                self.itemHorizontalSpace = (totalWidth-self.itemIntrinsicWidth*self.listCount)/(self.listCount-1);
            }
            else {
                CGFloat minHorizontalSpace = self.itemHorizontalSpace;
                NSInteger listCount = (totalWidth+minHorizontalSpace)/(self.itemIntrinsicWidth+minHorizontalSpace);
                if (listCount>1) {
                    self.itemHorizontalSpace = (totalWidth - self.itemIntrinsicWidth*listCount)/(listCount-1);
                }
                self.listCount = listCount;
            }
            
        }
        else {
            self.itemIntrinsicWidth = (totalWidth - self.itemHorizontalSpace*(self.listCount - 1))/self.listCount;
        }
        CGFloat currentLeft = self.marginInsets.left;
        CGFloat currentTop = self.marginInsets.top;
        for (NSInteger i=0; i<self.itemCount; i++) {
            CGFloat width = self.itemIntrinsicWidth;
            if (self.customItemWidthAtIndexBlock) {
                width = self.customItemWidthAtIndexBlock(i);
            }
            CGFloat height = self.itemIntrinsicHeight;
            if (self.itemIntrinsicHeightRatioToWidth) {
                height = width*self.itemIntrinsicHeightRatioToWidth;
            }
            if (currentLeft+width>self.totalWidth-self.marginInsets.right) {
                currentLeft = self.marginInsets.left;
                currentTop += (self.itemVerticalSpace+height);
            }
            [itemFramesArray addObject:[NSValue valueWithCGRect:CGRectMake(currentLeft, currentTop, width, height)]];
            if (i==self.itemCount-1) {
                _totalHeight = currentTop += (height+self.marginInsets.bottom);
            }
            else {
                currentLeft += (width+self.itemHorizontalSpace);
            }
        }
    }
    _itemFramesArray = [NSArray arrayWithArray:itemFramesArray];
}

CGSize _TFScreenSize() {
    CGSize size = [UIScreen mainScreen].bounds.size;
    if (size.height < size.width) {
        CGFloat tmp = size.height;
        size.height = size.width;
        size.width = tmp;
    }
    return size;
}

@end
